package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kaoM1Jy
 * @date 2025/10/9 15:55
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/courses")
@SuppressWarnings("all")
public class CourseController {
    @Autowired
    private CourseService courseService;


    @GetMapping("subject/{subject}")
    public Result getBySubject(@PathVariable Integer subject) {
        log.info("查询课程：{}", subject);
        List<Course> list= courseService.getBySubject(subject);
        return Result.success(list);
    }

    @GetMapping("/list")
    public Result getAll() {
        log.info("查询所有课程");
        List<Course> list= courseService.getAll();
        return Result.success(list);
    }

    @PutMapping
    public Result update1(@RequestBody Course course) {
        log.info("修改课程：{}", course);
        courseService.update1(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询课程：{}", id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }



    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("添加课程：{}", course);
        courseService.add(course);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteByID(@PathVariable Integer id) {
        log.info("删除课程：{}", id);
        courseService.deleteByID(id);
        return Result.success();
    }


    @GetMapping
    public Result page(@RequestParam(required = false) String name,
                       @RequestParam(required = false) Integer subject,
                       @RequestParam(required = false) Integer target,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询课程, 参数: name: {}, subject: {}, target: {}, page: {}, pageSize: {}", name, subject, target, page, pageSize);
        PageResult<Course> pageResult = courseService.page(name, subject, target, page, pageSize);
        return Result.success(pageResult);

    }
}
